using System.Text.Json.Serialization;
using System.Text.Json;
using System;

namespace TaskProcessDemo
{
    /// <summary>
    /// 常量类
    /// </summary>
    public static class Constant
    {
        public const string TaskServerApiPrefix = "/api/v1/task/basic";

        /// <summary>
        /// 获取更新任务进度的API
        /// </summary>
        public static string GetUpdateProgressURL(int servePort)
        {
            //http://127.0.0.1:8089/api/v1/task/progress/update
            return $"http://127.0.0.1:{servePort}{TaskServerApiPrefix}/progress/update";
        }

        /// <summary>
        /// 获取更新任务状态的API
        /// </summary>
        public static string GetUpdateStatusURL(int servePort)
        {
            //http://127.0.0.1:8089/api/v1/task/status/update
            return $"http://127.0.0.1:{servePort}{TaskServerApiPrefix}/status/update";
        }
    }

    /// <summary>
    /// 任务状态枚举
    /// </summary>
    [JsonConverter(typeof(JsonStringEnumConverter))]
    public enum TaskStatus
    {
        PENDING,
        RUNNING,
        COMPLETED,
        FAILED,
        CANCELED
    }

    /// <summary>
    /// 任务信息参数
    /// </summary>
    [Serializable]
    public class TaskArgs
    {
        /// <summary>
        /// 任务ID
        /// </summary>
        [JsonPropertyName("task_id")]
        public required string TaskId { get; set; }
        /// <summary>
        /// 任务名称
        /// </summary>
        [JsonPropertyName("task_name")]
        public required string TaskName { get; set; }
        /// <summary>
        /// 任务状态
        /// </summary>
        [JsonPropertyName("task_status")]
        public required TaskStatus Status { get; set; }
    }
    /// <summary>
    /// 任务内部执行参数
    /// </summary>
    [Serializable]
    public class ParamArgs
    {
        /// <summary>
        /// 参数ID
        /// </summary>
        [JsonPropertyName("id")]
        public required string Id { get; set; }
        /// <summary>
        /// 参数名称
        /// </summary>
        [JsonPropertyName("name")]
        public required string Name { get; set; }
        /// <summary>
        /// 参数数组
        /// </summary>
        [JsonPropertyName("infos")]
        public required string[] Infos { get; set; }
    }

    /// <summary>
    /// 当前进程启动时传递的参数模型
    /// </summary>
    public class ProcessArgs
    {
        /// <summary>
        /// 任务服务端口
        /// </summary>
        public int ServePort { get; set; }
        /// <summary>
        /// 任务信息参数
        /// </summary>
        public TaskArgs TaskArgs { get; set; }
        /// <summary>
        /// 任务内部执行参数
        /// </summary>
        public ParamArgs ParamArgs { get; set; }

        /// <summary>
        /// 构造函数，将JSON字符串转换为对象
        /// </summary>
        /// <param name="servePort">任务服务端口</param>
        /// <param name="taskArgsJson">任务信息参数JSON字符串</param>
        /// <param name="paramArgsJson">任务内部执行参数JSON字符串</param>
        public ProcessArgs(int servePort, string taskArgsJson, string paramArgsJson)
        {
            ServePort = servePort;
            try
            {
                // 将JSON字符串转换为TaskArgs对象
                if (!string.IsNullOrEmpty(taskArgsJson))
                {
                    TaskArgs = JsonSerializer.Deserialize<TaskArgs>(taskArgsJson);
                }
            }
            catch (JsonException ex)
            {
                throw new ArgumentException($"taskArgs JSON反序列化失败: {ex.Message}", ex);
            }
            try
            {
                // 将JSON字符串转换为ParamArgs对象
                if (!string.IsNullOrEmpty(paramArgsJson))
                {
                    ParamArgs = JsonSerializer.Deserialize<ParamArgs>(paramArgsJson);
                }
            }
            catch (JsonException ex)
            {
                throw new ArgumentException($"paramArgs JSON反序列化失败: {ex.Message}", ex);
            }
        }
    }



    /// <summary>
    /// 任务进度请求模型
    /// </summary>
    public class TaskProgressRequest
    {
        /// <summary>
        /// 任务ID
        /// </summary>
        [JsonPropertyName("task_id")]
        public required string TaskId { get; set; }
        /// <summary>
        /// 任务进度，0~0.99之间的浮点数 ,最后完成时为1.0且发送任务状态必须为COMPLETED
        /// </summary>
        [JsonPropertyName("progress")]
        public float Progress { get; set; } = 0.0f;
        /// <summary>
        /// 任务进度描述
        /// </summary>
        [JsonPropertyName("message")]
        public string Message { get; set; } = "";
    }

    /// <summary>
    /// 任务状态请求模型
    /// </summary>
    public class TaskStatusRequest
    {
        /// <summary>
        /// 任务ID
        /// </summary>
        [JsonPropertyName("task_id")]
        public required string TaskId { get; set; }
        /// <summary>
        /// 任务状态
        /// </summary>
        [JsonPropertyName("status")]
        public required TaskStatus Status { get; set; }
        /// <summary>
        /// 任务状态描述
        /// </summary>
        [JsonPropertyName("message")]
        public string Message { get; set; } = "";
    }
}
